#include "Actor.h"
#include "Scene.h"

namespace egg{

    Actor::Actor(){
        mDepth=0;
        mParent=0;
        mBoundingDirty=false;
        mTransformDirty=false;
    }

    void Actor::addTo(Actor* _parent){
        mParent=_parent;
        mParent->mChildren.push_back(this);
    }

    void Actor::updateDepth(){
        mDepth=0;
        Actor* parent=mParent;
        while(parent){
            ++mDepth;
            parent=mParent->getParent();
        }
    }

    void Actor::onUpdateBounding(){
        mBoundingDirty=false;
    }

    void Actor::onUpdateTransform(){
        mTransformDirty=false;
    }

    void Actor::onFrameEnd(const FrameEvent& _evt){

    }

    void Actor::onFrameBegin(const FrameEvent& _evt){

    }

    void Actor::notifyBoundingDirty(){
        while(mParent){
            mBoundingDirty=true;
            getScene()->addBoundingUpdateListener(this);
            mParent->notifyBoundingDirty();
        }
    }

    void Actor::notifyTransformDirty(){
        for(uint32 i=0;i<mChildren.size();i++){
            mTransformDirty=true;
            getScene()->addTransformUpdateListener(this);
            mChildren[i]->notifyTransformDirty();
        }
    }

    void Actor::onRendering(RendererPtr& _renderer){

    }

    Scene* Actor::getScene(){
        return mParent->getScene();
    }

    vec4 Actor::getWorldPosition(){
        return mParent->getWorldPosition();
    }

    vec4 Actor::getParentPosition(){
        return mParent->getParentPosition();
    }

    const mat4& Actor::getLocal2WorldMatrix(){
        return mParent->getLocal2WorldMatrix();
    }

    const mat4& Actor::getLocal2ParentMatrix(){
        return mParent->getLocal2ParentMatrix();
    }

}